package Org.Multiuni.Android.MediaPlayer;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;

public class ListSong extends ListActivity{
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initializeComponent();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		launchMediaPlayer(position);
	}
	
	private void initializeComponent() {
		mCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				null, null, null, MediaStore.Audio.Media.TITLE + " ASC");
		adapter = new ListSongAdapter(this, mCursor);
		setListAdapter(adapter);
		
	}
	
	private void launchMediaPlayer(int _position) {
		Intent mediaPlayerIntent = new Intent(getApplicationContext(), MediaPlayerActivity.class);
		mediaPlayerIntent.putExtra("ItemId",_position);
		startActivity(mediaPlayerIntent);
	}
	
	private Cursor mCursor;
	private ListSongAdapter adapter;
	
    
}
		
//		Uri _uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
		
//		String _name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//		Uri _uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id-1);
//		String _artist = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
		
//		mRecord[ID_OF_SONG] = _uri.toString();
//		mRecord[SONG_NAME_COL] = _name;
//		mRecord[ARTIST_COL] = _artist;
//		Bundle b = new Bundle();
//		mediaPlayerIntent.putExtra("idOfSong", _uri.toString());
//		mediaPlayerIntent.putExtra("record", mRecord);
//		startActivity(mediaPlayerIntent);
		
//		Toast.makeText(getApplicationContext(),""+position+" "+id, Toast.LENGTH_LONG).show();
//	}


	
//	@Override
//	protected void onStart() {
//		super.onStart();
//		
//		initializeComponents();
//		List<Map<String, Object>> data = getData();
//	}
//	
////	private List<Map<String,Object><>> getData
//	
//	
////	private List<Map<String, Object>> 
//	
//	private void initializeComponents() {
//		
//		mRecord = new String[COLUMN];
//		mCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
//				null, null, null, MediaStore.Audio.Media.TITLE + " ASC");
//		
//		columns = new String[] {
//        		MediaStore.Audio.Media.TITLE,
//        		MediaStore.Audio.Media.ARTIST,
//        		MediaStore.Audio.Media.DURATION
//        };
//		
//		ids = new int[] {
//	        	R.id.title,
//	        	R.id.artist,
//	        	R.id.duration,
//        };
//		
//		
//		adapter = new SimpleCursorAdapter(this, 
//				R.layout.list_song_layout, mCursor, columns, ids);
////		mData = getData();
////		adapter = new SimpleAdapter(getApplicationContext(), mData, R.layout.list_song_layout,
////				columns, ids);
//		setListAdapter(adapter);
//	}
//	
//	
//	
//    private List<Map<String, Object>> getData() {
//        List<Map<String, Object>> _data = new ArrayList<Map<String, Object>>();
//
//        while(mCursor.moveToNext()) {
//        	Map<String, Object> map = new HashMap<String, Object>();
//	        String duration = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
//	        long value = Long.parseLong(duration);
//	        
//	        String title = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//	        String artist = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
//	        
//	        map.put(MediaStore.Audio.Media.TITLE, title);
//	        map.put(MediaStore.Audio.Media.ARTIST, artist);
//	        map.put(MediaStore.Audio.Media.DURATION, createHMFormatfromM(value));
//	        
//	        _data.add(map);
//        }
//        
//        return _data;
//    }
//	
//	
//	private String createHMFormatfromM(long _value) {
//		
//		String _res = null;
//		long _tempsecond, _second,_minute = 0;
//		
//		_tempsecond = _value/CONSTANT1;
//		_second = _tempsecond%CONSTANT2;
//		_minute = _tempsecond/CONSTANT2;
//		
//		_res = String.valueOf(_minute) + ":" + String.valueOf(_second);
//		
//		return _res;
//	}
//	
//	@Override
//	protected void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
//		
//		mCursor.moveToPosition(position);
//		
//		Intent mediaPlayerIntent = new Intent(getApplicationContext(), MediaPlayerActivity.class);
//		
////		String title = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//		Uri _uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
//		
//		String _name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//		String _artist = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
//		
//		mRecord[ID_OF_SONG] = _uri.toString();
//		mRecord[SONG_NAME_COL] = _name;
//		mRecord[ARTIST_COL] = _artist;
////		Bundle b = new Bundle();
////		mediaPlayerIntent.putExtra("idOfSong", _uri.toString());
//		mediaPlayerIntent.putExtra("record", mRecord);
//		startActivity(mediaPlayerIntent);
//		
//		Toast.makeText(getApplicationContext(),_uri.toString(), Toast.LENGTH_LONG).show();
//	}
//	
//	private Cursor mCursor;
//	private String[] columns;
//	private int[] ids;
//	private final static int CONSTANT1 = 1000;
//	private final static int CONSTANT2 = 60;
//	private List<Map<String, Object>> mData;
//	private String[] mRecord;
//	private static final int ID_OF_SONG = 0;
//	private static final int SONG_NAME_COL = 1;
//	private static final int ARTIST_COL = 2;
//	private static final int COLUMN = 3;
//	private SimpleCursorAdapter adapter;
////	private SimpleAdapter adapter;
//}
//}